/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.smart.hawk.utilities;

import java.awt.Component;
import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.SwingWorker;

/**
 *
 * @author Andres
 */
public  class PanelCargando extends javax.swing.JDialog {

    private int contador = 0;
    private SwingWorker worker;
    private PanelCargando panel;
    private Component parent;

    public PanelCargando() {
    }
    
    

    public class PropertyChangeHandler implements PropertyChangeListener {

        public PropertyChangeHandler() {
        }

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            
            System.out.println(evt.getPropertyName());
            switch (evt.getPropertyName()) {
                case "state":
                    SwingWorker.StateValue state = (SwingWorker.StateValue) evt.getNewValue();
                    switch (state) {
                        case STARTED:
                            PanelCargando.this.dispose();
                    }
                    break;
                case "progress":
                    PanelCargando.this.progressBar.setValue(Integer.parseInt(evt.getNewValue().toString()));
                    if (Integer.parseInt(evt.getNewValue().toString()) == 100) {
                        PanelCargando.this.dispose();
                    }
                    break;
            }
        }
    }

    private void setParent(Component parentt) {

        this.parent = parentt;
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                worker = new SwingWorker() {

                    @Override
                    protected Object doInBackground() throws Exception {

                        int i = 0;
                        while (i == 0) {
                            if (contador == 0) {
                                worker.cancel(true);
                                PanelCargando.this.hide();
                                PanelCargando.this.dispose();
                            }
                        }
                        return null;
                    }
                };
                mostrarPanel(parent, worker);
            }
        });

    }

    /**
     * Creates new form PanelCargando
     */
    private PanelCargando(Component parent, SwingWorker worker) {
        super();
        initComponents();
        worker.addPropertyChangeListener(new PropertyChangeHandler());
        switch (worker.getState()) {
            case DONE:
                worker.execute();
        }
    }

    private void close() {
        setVisible(false);
        dispose();
    }

    private void mostrarPanel(Component parent, SwingWorker worker) {
        PanelCargando panelCargando = new PanelCargando(parent, worker);
        panelCargando.setLocationRelativeTo(parent);
        panelCargando.setVisible(true);
    }

    public synchronized void ocultarPanel() {
        contador--;
        if (contador < 0) {
            System.err.println("El Componente de PanelCargando() tiene llamados no sincronizados");
            contador = 0;
        }
    }

    public synchronized void mostrarPanel(Component parent) {
        contador++;
        setParent(parent);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        progressBar = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setFocusCycleRoot(false);
        setModal(true);
        setResizable(false);
        setUndecorated(true);

        progressBar.setBackground(new java.awt.Color(255, 255, 255));
        progressBar.setForeground(new java.awt.Color(204, 204, 204));
        progressBar.setFocusTraversalPolicyProvider(true);
        progressBar.setIndeterminate(true);
        progressBar.setInheritsPopupMenu(true);
        progressBar.setOpaque(true);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JProgressBar progressBar;
    // End of variables declaration//GEN-END:variables
}
